package com.atzon.DAO;

import com.atzon.model.Dept;
import com.atzon.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;
import java.util.List;

public class BaseDAO {
    public Dept findDeptByName(String deptName) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Dept dept = new Dept();
        try {
            String sql = "select * from dept where name = ? ";
            dept = qr.query(sql, new BeanHandler<Dept>(Dept.class), deptName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dept;
    }

    public List<Object[]> findAll() {
        try {
            QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
            List<Object[]> list = qr.query("select * from user", new ArrayListHandler());
            if (list != null) {
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
